import emitter from "../emitter";
import { planeBullet } from "../gameMrg";
import playerDataInfo from "../playerData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class bullet extends cc.Component {
    @property(cc.Node)
    nodeBullet: cc.Node = null
    onLoad() {
        this.initData();
        this.addEventListener();
    }

    addEventListener() {
        emitter.on("setBulletLevel", this.setBulletLevel.bind(this));
    }

    onDisable() {
        emitter.off("setBulletLevel", this.setBulletLevel.bind(this));
    }

    public bulletHarm;
    private maxPosiY;
    initData() {
        this.maxPosiY = cc.winSize.height;
        //@ts-ignore
        this.bulletHarm = playerDataInfo.gameData.playerLevel;
        if (!this.bulletHarm) this.bulletHarm = 1;
        //@ts-ignore
        if (playerDataInfo.gameData.bulletType == "A") {
            //@ts-ignore
            this.setBulletLevel(playerDataInfo.gameData.bulletLevel)
        }
    }

    //根据不同等级设置不同发射数量及角度
    setBulletLevel(level) {
        switch (Number(level)) {
            case 1:
                this.computeBulletAngle(2, 15);
                break;
            case 2:
                this.computeBulletAngle(4, 30);
                break
            case 3:
                this.computeBulletAngle(8, 45);
                break
            case 4:
                this.computeBulletAngle(16, 60);
                break
            case 5:
                this.computeBulletAngle(24, 90);
                // this.computeBulletAngle(10, 360);
                break
            case 6:
                this.computeBulletAngle(32, 120);
                // this.computeBulletAngle(20, 360);
                break
            default:
                break;
        }
    }

    //计算角度
    computeBulletAngle(bulletNum, angle) {
        let minAngle = angle / 2;
        let bulletAngle = angle / (bulletNum - 1);
        emitter.emit("sendBullet", bulletNum, -minAngle, bulletAngle)
    }

    private isStop = false;
    update() {
        if (this.isStop) return
        this.nodeBullet.y += planeBullet.SPEED;
        if (this.maxPosiY < this.nodeBullet.y) {
            this.isStop = true;
            emitter.emit("recycle", this.node);
        }
    }
}
